package com.cs.framework.handler;


import com.cs.core.exception.BusinessException;
import com.cs.core.exception.ParamException;
import com.cs.framework.common.HttpCode;
import com.cs.framework.domain.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @package com.cs.feature.web.handler
 * @class GlobalExceptionHandler
 * @desc  全局异常处理
 * @author wind
 * @date    2023/8/4 16:14
 * @version v1.0
 */
//@ControllerAdvice(annotations = {RestController.class, Controller.class})
//@ResponseBody
public class GlobalExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 异常捕获
     * @param exception
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public R paramException(ParamException exception){
        logger.error("GlobalExceptionHandler.paramException", exception);
        return HttpCode.paramError(exception.getErrMsg());
    }

    /**
     * 异常捕获
     * @param exception
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public R businessException(BusinessException exception){
        logger.error("GlobalExceptionHandler.businessException", exception);
        return HttpCode.fail(exception.getErrMsg());
    }

    /**
     * 异常捕获
     * @param exception
     * @return
     */
    @ExceptionHandler(Exception.class)
    public R exception(Exception exception){
        logger.error("GlobalExceptionHandler.exception", exception);
        return HttpCode.serverError();
    }
}
